/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import java.util.List;
import models.Activation;
import models.AssignmentHandin;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Achie
 */
public class AssignmentHandinDao {

    private final Session session;

    public AssignmentHandinDao(Session session) {
        this.session = session;
    }

    public List<AssignmentHandin> findAll() {
        Transaction tx = session.beginTransaction();
        List<AssignmentHandin> list = session.createQuery("from AssignmentHandin").list();
        tx.commit();
        return list;
    }

    public boolean save(AssignmentHandin assignmentHandin) {
        try {
            Transaction tx = session.beginTransaction();
            session.save(assignmentHandin);
            tx.commit();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean update(AssignmentHandin assignmentHandin) {
        try {
            Transaction tx = session.beginTransaction();
            session.update(assignmentHandin);
            tx.commit();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public AssignmentHandin get(long id) {
        return (AssignmentHandin) session.get(AssignmentHandin.class, id);
    }

    public void delete(Activation activation) {
        Transaction tx = session.beginTransaction();
        session.delete(activation);
        tx.commit();
    }

    //get the assignment objects by the students ids
    public List<AssignmentHandin> findAllByStudent(long id) {
        Transaction tx = session.beginTransaction();
        String sql = "select id from assignment_handin where student_id=:id";
        Query query = session.createSQLQuery(sql).setParameter("id", id);
        List idlist = query.list();
        ArrayList<AssignmentHandin> list = new ArrayList<AssignmentHandin>();
        for (int i = 0; i < idlist.size(); i++) {
            AssignmentHandin assignmenthandin = get(Long.parseLong(idlist.get(i).toString()));
            list.add(assignmenthandin);
        }
        System.out.println(list.size());
        tx.commit();
        return list;
    }

    //get the assignment (in a course) objects by the studentid and course id 
    public List<AssignmentHandin> findAllByStudentAndCourse(long studentid, long courseid) {
        Transaction tx = session.beginTransaction();
        String sql = "SELECT id FROM assignment_handin WHERE assignment_id IN (SELECT id FROM files INNER JOIN lesson ON lesson_id=lesson_lesson_id WHERE course_id= :courseid) AND student_id= :studentid";
        Query query = session.createSQLQuery(sql).setParameter("studentid", studentid).setParameter("courseid", courseid);
        List idlist = query.list();
        ArrayList<AssignmentHandin> list = new ArrayList<AssignmentHandin>();
        for (int i = 0; i < idlist.size(); i++) {
            AssignmentHandin assignmenthandin = get(Long.parseLong(idlist.get(i).toString()));
            list.add(assignmenthandin);
        }
        System.out.println(list.size());
        tx.commit();
        return list;
    }

    //get the assignment objects by the student id and assignment id
    public List<AssignmentHandin> findAllByStudentAndAssignment(long studentid, long assignmentid) {
        Transaction tx = session.beginTransaction();
        String sql = "SELECT id FROM assignment_handin WHERE student_id= :studentid AND assignment_id= :assignmentid";
        Query query = session.createSQLQuery(sql).setParameter("studentid", studentid).setParameter("assignmentid", assignmentid);
        List idlist = query.list();
        ArrayList<AssignmentHandin> list = new ArrayList<AssignmentHandin>();
        for (int i = 0; i < idlist.size(); i++) {
            AssignmentHandin assignmenthandin = get(Long.parseLong(idlist.get(i).toString()));
            list.add(assignmenthandin);
        }
        System.out.println(list.size());
        tx.commit();
        return list;
    }
}
